package com.h3c.nem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.annotation.CheckRole;
import com.h3c.nem.dto.TbWorkSecurityMiddleDTO;
import com.h3c.nem.enums.RoleType;
import com.h3c.nem.service.ITbWorkSecurityMiddleService;
import com.h3c.nem.utils.ReturnCodeData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 工作履行地和参保地中间表控制层
 *
 * @author chenyifan
 * @since 2022-06-16 09:53:00
 */
@Api(tags = "WorkSecurityMiddle  接口")
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/workSecurityMiddle")
public class TbWorkSecurityMiddleController {
    /**
     * 服务对象
     */
    private final ITbWorkSecurityMiddleService workSecurityMiddleService;


    /**
     * 获取可选择的参保地选项
     * @return
     */
    @GetMapping("/getOptionalCoverage")
    public Result<List<String>> getOptionalCoverage(){
        try {
            return new Result(true, StatusCodeEnum.NORMAL,workSecurityMiddleService.getOptionalCoverage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 400,"获取参保地选项失败,原因:"+e.getMessage());
        }
    }

    /**
     * 列表分页模糊查询
     * @param paramsDTO
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "WorkSecurityMiddleDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryFuzzyListForPage(@ModelAttribute TbWorkSecurityMiddleDTO paramsDTO,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<TbWorkSecurityMiddleDTO> resultDTOList = workSecurityMiddleService.selectFuzzyByDto(paramsDTO);
            PageInfo<TbWorkSecurityMiddleDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 列表分页精确查询
     * @param paramsDTO
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "WorkSecurityMiddleDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryListForPage(@ModelAttribute TbWorkSecurityMiddleDTO paramsDTO,
                                                       @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<TbWorkSecurityMiddleDTO> resultDTOList = workSecurityMiddleService.selectFuzzyByDto(paramsDTO);
            PageInfo<TbWorkSecurityMiddleDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行列表分页查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 数据插入
     * @param paramsDTO
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "WorkSecurityMiddle", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> save(@RequestBody TbWorkSecurityMiddleDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = workSecurityMiddleService.insertDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据插入成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 数据修改
     * @param paramsDTO
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "WorkSecurityMiddle", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> update(@RequestBody TbWorkSecurityMiddleDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = workSecurityMiddleService.updateDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据修改成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "WorkSecurityMiddle", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> delete(@RequestBody TbWorkSecurityMiddleDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = workSecurityMiddleService.deleteDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据删除成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 工作常驻地和参保地导入
     * @param file
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})    @ApiOperation(value = "工作常驻地和参保地导入", notes = "工作常驻地和参保地导入")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "导入数据文件", required = true, dataType = "MultipartFile", paramType = "body")})
    @RequestMapping(value = "/importWorkSecurityMiddle", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> importWorkSecurityMiddle(@RequestParam(value="file") MultipartFile file) {
//        Integer result = sdUserService.importUserRoleList(file);
        Integer result = workSecurityMiddleService.importUserNew(file);
        JSONObject data = new JSONObject();
        data.put("data", result);
        data.put("msg", "用户角色导入");
        data.put("code", ReturnCodeData.SUCCESS);

        log.info("执行 导入工作常驻地和参保地完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

}

